package com.spsoft.vatoutinv.service.translate.impl;

import com.spsoft.system.service.DefBizdictService;
import com.spsoft.vatoutinv.constants.CommonConstant;
import com.spsoft.vatoutinv.service.translate.TranslateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author wangy
 * @date 2023/11/27 16:16
 */
@Service
public class DictTranslate implements TranslateService {

    @Resource
    private DefBizdictService defBizdictService;

    @Override
    public String translate(String dictType, String code, String format, String key) {
        // 获取字典类型集合
        Map<String, String> defBIzdictNameByLxdm = defBizdictService.getDefBIzdictNameByLxdm(code);
        // 如果待翻译的字段值包含“、”，则表示是多个码值，需要分开翻译并拼接
        if (StringUtils.isNotBlank(key) && key.contains(CommonConstant.STRING_COMMA)) {
            String[] keyList = key.split(CommonConstant.STRING_COMMA);
            StringBuilder stringBuilder = new StringBuilder();
            for (String splitKey : keyList) {
                String value = defBIzdictNameByLxdm.get(splitKey);
                stringBuilder.append(value).append(CommonConstant.STRING_COMMA);
            }
            String returnMsg = stringBuilder.toString();
            if (returnMsg.endsWith(CommonConstant.STRING_COMMA)) {
                returnMsg = returnMsg.substring(0, returnMsg.lastIndexOf(CommonConstant.STRING_COMMA));
            }
            return returnMsg;
        } else {
            return defBIzdictNameByLxdm.get(key);
        }
    }

    public String translate(String code, String value) {
        // 获取字典类型集合
        Map<String, String> defBIzdictNameByLxdm = defBizdictService.getDefBIzdictNameByLxdm(code);
        return defBIzdictNameByLxdm.get(value);
    }
}
